/*
 * 文件名：consumableTypeInfoServiceImpl.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：wuliquan
 * 修改时间：2013-2-25
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.consumables.consumablesInfo.service.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.dao.DataAccessException;

import com.wootide.cms.common.CMSRuntimeException;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.common.util.CommonUtil;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.consumables.consumablesInfo.dao.ConsumablesTypeInfoDao;
import com.wootide.cms.service.consumables.consumablesInfo.pojo.ConsumablesTypeInfo;
import com.wootide.cms.service.consumables.consumablesInfo.service.ConsumablesTypeInfoService;
import com.wootide.cms.service.consumables.consumablesInfo.transaction.ConsumablesTypeInfoTransaction;

/**
 * 易耗品类型业务层接口实现
 * 〈功能详细描述〉
 * @author wuliquan
 * @version 2013-2-25
 * @see ConsumablesTypeInfoServiceImpl
 * @since
 */
public class ConsumablesTypeInfoServiceImpl implements ConsumablesTypeInfoService
{
    /**
     * 运行日志实例
     */
    private RunLog runLog;
    
    /**
     * 易耗品数据访问层实例
     */
    private ConsumablesTypeInfoDao consumablesTypeInfoDao;
    
    /**
     * 易耗品事务层实例
     */
    private ConsumablesTypeInfoTransaction consumablesTypeInfoTransaction;
    
    public Result addOneConsumableTypeInfo(ConsumablesTypeInfo consumablesTypeInfo,
            HttpServletRequest request)
    {
     // 打印方法入口参数
        runLog.debug("consumablesTypeInfo=" + consumablesTypeInfo);
        Result result = new Result();
        try
        {
            consumablesTypeInfoTransaction.addOneConsumableTypeInfo(consumablesTypeInfo);
            runLog.info("Add ConsumablesTypeInfo Success! Input data:" + consumablesTypeInfo, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        catch (DataAccessException e)
        {
            e.printStackTrace();
            //数据库异常
            runLog.error("Add ConsumablesTypeInfo Failed! Input data:" + consumablesTypeInfo,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //运行时异常
            runLog.info("Add ConsumablesTypeInfo Failed! Input Data:" + consumablesTypeInfo
                    + " Error Message:" + e.getMessage(), request);
            
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //未知异常
            runLog.error("Add ConsumablesTypeInfo Failed!Input data:" + consumablesTypeInfo,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public Result checkContentWhenAdd(String content)
    {
     // 打印方法入口参数
        runLog.debug("content=" + content);
        
        int num = consumablesTypeInfoDao.findNumByConsumablesTypeInfoName(content);
        Result result = new Result();
        // 如果已存在
        if (num > 0)
        {
            runLog.info("Illegal!ConsumableTypeInfo Has Existed! Input Data:" + content);
            
            result.setResultCode(ErrorCodes.Consumables.ConsumablesInfo.COMSUMABLESINFO_EXIST);
        }
        // 如果不存在
        else
        {
            runLog.info("Legal!ConsumableTypeInfo Not Existed! Input Data:" + content);
            
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        return result;
    }
    
    public Result delConsumableTypeInfo(Integer[] innerIndexs,
            HttpServletRequest request)
    {
     // 打印方法入口参数
        runLog.debug("innerIndexs = " + CommonUtil.toString(innerIndexs));
        
        Result result = new Result();
        
        result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        // 定义删除失败的条数
        int delErrorNum = 0;
        
        for (Integer innerIndex : innerIndexs)
        {
            ConsumablesTypeInfo consumableTypeInfo = null;
            try
            {
                // 调用删除路由事务层
                consumableTypeInfo = consumablesTypeInfoTransaction.delOneConsumableTypeInfo(innerIndex);
                runLog.info("Delete consumableTypeInfo Success! consumableTypeInfo = " + consumableTypeInfo,
                        request);
                
            }
            catch (DataAccessException e)
            {
                //数据库异常
                delErrorNum++;
                runLog.error("Delete consumableTypeInfo Failed! Innerindex = " + innerIndex,
                        e,
                        request);
                
                result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
                
            }
            catch (CMSRuntimeException e)
            {
                //运行时异常
                runLog.info("Delete consumableTypeInfo Failed! Innerindex = " + innerIndex
                        + " Error Message:" + e.getMessage(), request);
                result.setResultCode(e.getErrorCode());
                
            }
            catch (Exception e)
            {
                //未知异常
                delErrorNum++;
                runLog.error("Delete consumableTypeInfo Failed!  Innerindex = " + innerIndex,
                        e,
                        request);
                result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
                
            }
        }
        // 返回失败的条数
        result.setDelErrorNum(delErrorNum);
        return result;
    }
    
    public Result modOneConsumableTypeInfo(ConsumablesTypeInfo consumableType,
            HttpServletRequest request)
    {
     // 打印方法入口参数
        runLog.debug("consumableType=" + consumableType);
        Result result = new Result();
        try
        {
            ConsumablesTypeInfo oldconsumableTypeInfo = consumablesTypeInfoTransaction.modOneConsumableTypeInfo(consumableType);
            runLog.info("Mod consumableTypeInfo Success! Input data:" + consumableType
                    + "|oldDate:" + oldconsumableTypeInfo, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Mod consumableTypeInfo Failed! Input data:" + consumableType, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod consumableTypeInfo Failed! Input Data:" + consumableType
                    + " Error Message:" + e.getMessage(), request);
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //位置异常
            runLog.error("Mod consumableTypeInfo Failed!Input data:" + consumableType, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public Result queryForPages(Page page, QueryCondition queryConditon)
    {
     // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            queryConditon = null == queryConditon? new QueryCondition() : queryConditon;
            queryConditon.setObjClass(ConsumablesTypeInfo.class);
            QueryResults queryResults = consumablesTypeInfoTransaction.queryForPages(page,
                    queryConditon);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query consumableTypeInfo Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query consumableTypeInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query consumableTypeInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public Result queryAllConsumablesTypeInfos()
    {
        // 定义返回结果
        Result result = new Result();
        try
        {
            QueryResults queryResults = consumablesTypeInfoDao.findAllConsumablesTypeInfos();
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query consumableTypeInfo Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query consumableTypeInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query consumableTypeInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    public Result viewOneConsumableTypeInfo(Integer innerIndex)
    {
     // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        
        Result result = new Result();
        try
        {
            ConsumablesTypeInfo consumableType = consumablesTypeInfoTransaction.viewOneConsumableTypeInfo(innerIndex);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            result.setResultPojo(consumableType);
        }
        catch (DataAccessException e)
        {
            runLog.error("Find One consumableTypeInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod consumableTypeInfo Failed! Input Data:" + innerIndex
                    + " Error Message:" + e.getMessage());
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            runLog.error("Find One consumableTypeInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }

    public void setConsumablesTypeInfoDao(
            ConsumablesTypeInfoDao consumablesTypeInfoDao)
    {
        this.consumablesTypeInfoDao = consumablesTypeInfoDao;
    }

    public void setConsumablesTypeInfoTransaction(
            ConsumablesTypeInfoTransaction consumablesTypeInfoTransaction)
    {
        this.consumablesTypeInfoTransaction = consumablesTypeInfoTransaction;
    }
    
    

}
